import { NODE_ID_LABEL_MAP2 } from '../../../../../common/constants';
import warningCheck from './warningCheck';
import responseValidate from './responseValidate';
import fillMissingNode from './fillMissingNode';

export default function aviliable({ target, taskSettings, response, task }) {
	const {
		collect: { area },
		alert: { respTime, nodeCount, statusCodeRange },
	} = taskSettings;

	if (!response?.detail?.length) {
		task.status = 'failed';
	}

	let resp = responseValidate(response);
	resp = fillMissingNode(area, resp, NODE_ID_LABEL_MAP2);

	const { warnings, addition } = warningCheck({
		target,
		response: resp,
		rules: {
			respTime,
			nodeCount,
			statusCodeRange,
		},
	});
	const abnormalNodes = warnings.reduce((set, w) => {
		w.detail.area.forEach(node => set.add(node));
		return set;
	}, new Set());
	const nodeTotalCount = area.length || 0;
	const nodeAbnormalCount = abnormalNodes.size;

	return {
		warnings,
		addition: {
			...addition,
			nodeTotalCount,
			nodeAbnormalCount,
		},
	};
}
